<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>账户设置</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body>
<div id="app">
    <div class="app-container">
        <div class="box">
            <el-button type="info" round @click="goBack()">返回</el-button>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm" style="margin: auto auto">
                <el-form-item label="用户名:" prop="username">
                    <el-input v-model.number="username" style="width: auto" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="旧密码:" prop="oldPass">
                    <el-input v-model.number="ruleForm.oldPass" style="width: auto"></el-input>
                </el-form-item>
                <el-form-item label="新密码:" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off" style="width: auto"></el-input>
                </el-form-item>
                <el-form-item label="确认密码:" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" style="width: auto"></el-input>
                </el-form-item>
                <el-form-item>
                    <!--<el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>-->
                    <el-button type="primary" @click="submit()">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>



</div>
</body>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script>
    new Vue({
        el:"#app",
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var validateOldPass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入旧密码'));
                } else {

                    // if (this.ruleForm.checkPass !== '') {
                    //     this.$refs.ruleForm.validateField('checkPass');
                    // }

                    callback();
                }
            };
            return{
                username:'',
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    oldPass: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    oldPass: [
                        { validator: validateOldPass, trigger: 'blur' }
                    ]
                }



            };
        },
        created(){
            this.fetchData();


        },
        methods:{
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post(`/admin/updatePassword.do`,this.ruleForm).then(response=>{
                            if(response.data.code=='0'){

                                this.$alert('修改密码成功','页面提示');
                                window.location.href="/login.html";//返回到登录界面

                            }else{

                                this.$alert('操作失败','错误提示');
                            }

                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            fetchData(){
                axios.get(`/login/name.do`).then(response=>{
                    this.username=response.data.name;
                });

            },
            //自己写的提交
            submit(){
                axios.post(`/admin/updatePassword.do`,this.ruleForm).then(response=>{
                    if(response.data.code=='0'){

                    this.$message({
                        type: 'success',
                        message: '密码修改成功，请重新登录',
                        duration: 900 //持续时间,好像没用，可能跟浏览器有关，对于跳转到登录界面
                    });
                        window.location.href="/login.html";//返回到登录界面

                    }else{

                        this.$alert('操作失败','错误提示');
                    }

                });
            },
            goBack(){
                // window.history.forward();//待测试是否管用.没用，这个url是固定的localhost:9101/login
                window.location.href="/main.html";//这个对于这个项目就相当于返回原操作界面了
            }




        }

    })
</script>

</html>